<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
               :disabled="method==='view'"
               label-width="120px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="礼品名称" prop="giftName"
                          :rules="[
                  {required: true, message:'礼品名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.giftName" placeholder="请填写礼品名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="礼品价格" prop="price"
                :rules="[
                          {validator: validator.isNumber, trigger:'blur'},
                          {required: true, message:'礼品价格不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.price" placeholder="请填写礼品价格"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="礼品类型" prop="giftType"
                          :rules="[{required: true, message:'礼品类型不能为空', trigger:'blur'}]">
              <el-select v-model="inputForm.giftType" placeholder="请选择礼品类型" style="width: 100%;">
                <el-option v-for="item in $dictUtils.getDictList('gift_type')" :key="item.value" :label="item.label"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="method == 'add'" label="库存" prop="stock"
                          :rules="[
                  {validator: validator.isIntGtZero, trigger:'blur'},
                  {required: true, message:'库存不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.stock" placeholder="请填写库存"></el-input>
            </el-form-item>
            <el-form-item v-else label="调整库存" prop="stock"
                          :rules="[]">
              <el-input v-model="inputForm.stock" type="Number" @blur="tiaozhen" placeholder="请填写库存"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库存预警值" prop="inventoryAlertValue"
                          :rules="[ 
                  {validator: validator.isIntGtZero, trigger:'blur'},
                  {required: true, message:'库存预警值不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.inventoryAlertValue" placeholder="请填写库存预警提醒值"></el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="领取次数设置" prop="receiveTimesSetting"
                          :rules="[
                  {validator: validator.isIntGtZero, trigger:'blur'},
                  {required: true, message:'领取次数设置不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.receiveTimesSetting" placeholder="请填写领取次数设置" disabled></el-input>
            </el-form-item>
          </el-col> -->
          <el-col>
            <el-form-item label="是否多选一" prop="type">
              <el-radio v-model="type" label="0" size="mini" >否</el-radio>
              <el-radio v-model="type" label="1" size="mini" >是</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="type === '1'">
            <el-form-item label="专区批次" prop="batch">
              <el-select v-model="inputForm.batch" placeholder="专区批次">
                <el-option v-for="item in $dictUtils.getDictList('gift_batch')" :key="item.value" :label="item.label"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="礼品开始时间" prop="receiveStartTime"
                          :rules="[
                  {required: true, message:'礼品开始时间不能为空', trigger:'blur'}
                 ]">
              <el-date-picker
                style="width: 100%;"
                v-model="inputForm.receiveStartTime"
                type="month"
                value-format="yyyy-MM"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="礼品结束时间" prop="receiveEndTime"
                          :rules="[
                  {required: true, message:'礼品结束时间不能为空', trigger:'blur'}
                 ]">
              <el-date-picker
                style="width: 100%;"
                v-model="inputForm.receiveEndTime"
                type="month"
                value-format="yyyy-MM"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="是否复扫模式" prop="isRescan">
              <el-radio v-model="inputForm.isRescan" label="0" size="mini" >否</el-radio>
              <el-radio v-model="inputForm.isRescan" label="1" size="mini" >是</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="领取时间范围" prop="collectionTime"
                          :rules="[
                  {validator: validator.isIntGtZero, trigger:'blur'},
                  {required: true, message:'领取次数设置不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.collectionTime" placeholder="请填写领取时间范围单位"  ></el-input>
              
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12" v-if="inputForm.giftType == 1">
            <el-form-item label="领取时间起" prop="collectionStar"
                          :rules="[
                          {validator: validator.isIntGtZero, trigger:'blur'},
                 ]">
                 <el-input v-model="inputForm.collectionStar" placeholder="请填写领取时间起"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="inputForm.giftType == 1">
            <el-form-item label="领取时间止" prop="collectionEnd"
                          :rules="[
                          {validator: validator.isIntGtZero, trigger:'blur'},
                 ]">
                 <el-input v-model="inputForm.collectionEnd" placeholder="请填写领取时间止"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="24" v-if="inputForm.giftType == 1">
            <el-form-item label="" >
              <span style="color: red;font-size: 12px;">tips:如1号到5号可领取则，01-05，如是不连续的日期则01,03,05用英文逗号分隔即可,10以内的数字需要0开头，注意：只能一种</span>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="inputForm.giftType == 2">
            <el-form-item label="" >
              <span style="color: red;font-size: 12px;">tips:即几个月内可领取，如6个月则输入6即可</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="礼品图片" prop="giftPicture"
                          :rules="[
                  {required: true, message:'礼品图片不能为空', trigger:'blur'}
                 ]">
              <el-upload ref="giftPicture"
                         v-if="visible"
                         list-type="picture-card"
                         :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/vgiftinformation/vGiftInformation`"
                         :headers="{token: $cookie.get('token')}"
                         :on-preview="(file, fileList) => {
                        $alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
                          dangerouslyUseHTMLString: true,
                          showConfirmButton: false,
                          closeOnClickModal: true,
                          customClass: 'showPic'
                        });
                    }"
                         :on-success="(response, file, fileList) => {
                       inputForm.giftPicture = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                         :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.giftPicture = fileList.map(item => item.url).join('|')
                    }"
                         :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                         multiple
                         :limit="5"
                         :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                         :file-list="giftPictureArra">
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="礼品详情" prop="giftDetails"
                          :rules="[
                  {required: true, message:'礼品详情不能为空', trigger:'blur'}
                 ]">
              <WangEditor ref="giftDetailsEditor" v-model="inputForm.giftDetails"/>
            </el-form-item>
          </el-col>
         
          <!-- <el-col :span="12">
              <el-form-item label="备注信息" prop="remarks"
                  :rules="[
                   ]">
            <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
             </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import WangEditor from '@/components/editor/WangEditor'

export default {
  data() {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      giftPictureArra: [],
      stock: '',
      type:'0',
      inputForm: {
        id: '',
        giftName: '',
        giftPicture: '',
        giftDetails: '',
        stock: '',
        inventoryAlertValue: '',
        receiveTimesSetting: '1',
        receiveStartTime: '',
        receiveEndTime: '',
        remarks: '',
        deleteFlag: '0',
        giftType: '',
        batch: '',
        isRescan:'0',
        collectionTime:'',                          
        collectionStar :'',                                
        collectionEnd:'',
        price:'',
      }
    }
  },
  components: {
    WangEditor
  },
  methods: {
    tiaozhen(e) {
      console.log(e);
    },
    init(method, id) {
      this.method = method
      this.inputForm.id = id

      if (method === 'add') {
        this.title = `新建礼品信息列表`
      } else if (method === 'edit') {
        this.title = '修改礼品信息列表'
      } else if (method === 'view') {
        this.title = '查看礼品信息列表'
      }
      this.giftPictureArra = []
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        this.$refs.giftDetailsEditor.init('')
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          this.$http({
            url: `/vgiftinformation/vGiftInformation/queryById?id=${this.inputForm.id}`,
            method: 'get'
          }).then(({data}) => {
            this.inputForm = this.recover(this.inputForm, data.vGiftInformation)
            this.stock = this.inputForm.stock
            this.inputForm.stock = 0
            if (this.inputForm.batch){
              this.type = '1'
            }
            this.$refs.giftDetailsEditor.init(this.inputForm.giftDetails)
            this.inputForm.giftPicture.split('|').forEach((item) => {
              if (item.trim().length > 0) {
                this.giftPictureArra.push({
                  name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)),
                  url: item
                })
              }
            })
            this.loading = false
          })
        }
      })
    },
    // 表单提交
    doSubmit() {
      this.$refs['inputForm'].validate((valid) => {
        if (this.inputForm.id && this.inputForm.stock < 0 && this.stock < this.inputForm.stock * -1) {
          this.$message.error("调整库存不能大于剩余库存");
          return false
        }
        if (valid) {
          this.loading = true
          this.$http({
            url: `/vgiftinformation/vGiftInformation/save`,
            method: 'post',
            data: this.inputForm
          }).then(({data}) => {
            this.loading = false
            if (data && data.success) {
              this.visible = false
              this.$message.success(data.msg)
              this.$emit('refreshDataList')
            }
          })
        }
      })
    }
  }
}
</script>
